package com.zgjkhis.modules.system.service.mapper;

import com.zgjkhis.common.mapper.CoreMapper;
import com.zgjkhis.modules.basicsetting.service.dto.AdditionalQueryCriteria;
import com.zgjkhis.modules.basicsetting.service.dto.DrugInfoQueryCriteria;
import com.zgjkhis.modules.system.domain.Drug;
import com.zgjkhis.modules.system.service.dto.DrugQueryCriteria;
import com.zgjkhis.modules.system.service.dto.DrugStockWarningDto;
import com.zgjkhis.modules.system.service.dto.DrugTypeCountDto;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author yzq
 * @Description 药品
 * @Date 2021/6/23 9:48
 * @Param
 * @return
 **/
@Repository
public interface DrugMapper extends CoreMapper<Drug> {


    @Select("<script>select * from his_drug t where 1=1 and t.pharmacy_id=#{drug.pharmacyId} \n" +
            "<if test=\"drug.drugType !=null and drug.drugType != '' and drug.drugType!=7\">\n" +
            "\t and t.drug_type = #{drug.drugType}\n" +
            "</if>\n" +
            "<if test=\"drug.drugType !=null and drug.drugType != '' and drug.drugType ==7\">\n" +
            "\t and t.drug_type  in (3,4,5,6,8)\n" +
            "</if>\n" +
            "<if test=\"drug.drugName!=null and drug.drugName!=''\">\n" +
            "\t and (t.drug_name LIKE CONCAT('%',#{drug.drugName},'%') or t.pinyin LIKE CONCAT('%',#{drug.drugName},'%'))\n" +
            "</if>\n" +
            "<if test=\"drug.drugManufactor !=null and drug.drugManufactor !=''\">\n" +
            "\tand t.drug_manufactor LIKE CONCAT('%',#{drug.drugManufactor},'%')\n" +
            "</if>\n" +
            "<if test = \"drug.status != null\">\n" +
            "\tand t.status = #{drug.status}\n" +
            "</if>\n" +
            "<if test=\"drug.isStock == 0\">\n" +
            "\tand t.stock > 0\n" +
            "</if>\n" +
            "<if test=\"drug.isStock == 1\">\n" +
            "\tand IFNULL(t.stock,0) = 0\n" +
            "</if></script>")
    List<Drug> findAllByPageable(@Param("drug") DrugQueryCriteria criteria);

    @Select("<script>select * from(\n" +
            "\tselect * from (\n" +
            "\t\tselect t.drug_type,dt.label,count(1) total from (select * from his_drug where status=0 and pharmacy_id=#{drug.pharmacyId}) t\n" +
            "\t\tleft join (\n" +
            "\t\t\tselect dd.* from dict d \n" +
            "\t\t\tright join dict_detail dd on dd.dict_id=d.id\n" +
            "\t\t\twhere d.name='drug_type'\n" +
            "\t\t)dt on t.drug_type=dt.`value`\n" +
            "\t\tgroup by t.drug_type,dt.label\n" +
            "\t) a\n" +
            "\tunion \n" +
            "\tselect 0 drug_type,'全部' label, count(id) total from his_drug where status=0 and pharmacy_id=#{drug.pharmacyId}\n" +
            ")c order by c.drug_type asc</script>")
    List<DrugTypeCountDto> findCountByDrugType(@Param("drug") DrugQueryCriteria criteria);

    @Select("<script>select \n" +
            "\td.id, \n" +
            "\td.drug_name, \n" +
            "\td.stock, \n" +
            "\td.stock_warning,\n" +
            "\td.package_unit,\n" +
            "\td.moth_use\n" +
            "from his_drug d \n" +
            "\twhere \n" +
            "\t\td.`status` = 0 \n" +
            "\t\tand d.pharmacy_id = #{pharmacyId} \n" +
            "\t\tand d.stock_warning &gt; 0 \n" +
            "\t\tand d.stock &lt;= d.stock_warning" +
            "</script>")
    List<DrugStockWarningDto> findStockWarning(@Param("pharmacyId") String pharmacyId);

    @Update("update his_drug t set t.stock=(select IFNULL(sum(left_count),0) from ( \n" +
            "\t select * from his_drug_warehousing_detail where drug_id=#{id} and left_count>0\n" +
            " )a left join his_drug_warehousing dw on a.ware_housing_no=dw.ware_housing_no where dw.status=3 ),\n" +
            "t.min_stock=(select IFNULL(sum(min_left_count),0) from his_drug_warehousing_detail where drug_id=#{id} and left_count>0)\n" +
            "where id=#{id}")
    void updateStockById(@Param("id")Long id);

    @Update("update his_drug t set t.stock=(select IFNULL(sum(left_count*IFNULL(equivalent,1)),0) from ( \n" +
            "\tselect * from his_drug_warehousing_detail where drug_id=#{id} and left_count>0 \n" +
            ")a left join his_drug_warehousing dw on a.ware_housing_no=dw.ware_housing_no where dw.status=3 ),\n" +
            "t.min_stock=(select IFNULL(sum(min_left_count),0) from his_drug_warehousing_detail where drug_id=#{id} and left_count>0)\n" +
            "where id=#{id}")
    void updateStockByIdForDelivery(@Param("id")Long id);

    /**
     * 查询耗材
     * @Author yzq
     * @Description
     * @Date 2021/9/8 14:11
     * @param additionalQueryCriteria
     * @return java.util.List<com.zgjkhis.modules.system.domain.Drug>
     **/
    @Select("<script>select * from his_drug t where 1=1 and t.pharmacy_id=#{add.pharmacyId} \n" +
            "\t and t.drug_type = 5 \n"+
            "\t and (t.drug_name LIKE CONCAT('%',#{add.name},'%') or t.pinyin LIKE CONCAT('%',#{add.name},'%'))\n" +
            " and t.status=0</script>")
    List<Drug> finListByAdd(@Param("add") AdditionalQueryCriteria additionalQueryCriteria);

    /**
     * @Author yzq
     * @Description 根据处方类型，名称查询药材
     * @Date 2021/9/8 14:12
     * @param drugInfoQueryCriteria
     * @return java.util.List<com.zgjkhis.modules.system.domain.Drug>
     **/
    @Select("<script>select * from his_drug t where 1=1 and t.pharmacy_id=#{info.pharmacyId} and t.status=0 \n" +
            "and (t.drug_name LIKE CONCAT('%',#{info.name},'%') or t.pinyin LIKE CONCAT('%',#{info.name},'%'))           \n" +
            "<if test=\"info.type==1\">\n" +
            "\t and t.drug_type =1\n" +
            "</if>\n" +
            "<if test=\"info.type==3\">\n" +
            "\t and t.drug_type =2\n" +
            "</if>\n" +
            "<if test=\"info.type==2\">\n" +
            "\t and t.drug_type in('3','4')\n" +
            "</if>\n" +
            "<if test=\"info.type==4\">\n" +
            "\t and t.drug_type in('3','4')\n" +
            "</if>\n" +
            "<if test=\"info.type==5\">\n" +
            "\t and t.drug_type =6\n" +
            "</if></script>")
    List<Drug> findDrugByInfoType(@Param("info")DrugInfoQueryCriteria drugInfoQueryCriteria);
}
